// 左侧树配置 
/*
'{"callback":{"onClick":"clickTree"}, "view":{"showIcon": false,"addDiyDom-fn":"addDiyDom"}}'
*/
var dictTreeSetting = {
	callback: {
		onClick: clickTree,
		onRightClick: OnRightClick
	},
	view: {
		showIcon: true,
		isParent: "parent"
	}
};
/*tree点击事件*/
function clickTree(event, treeId, treeNode) {
	var pid = treeNode.id
	$("#lookup-pid").val(pid);
	$("button.query").trigger("click");
	var url = "/system/dict/children";
	$request.get(url, {
		data: {
			pid: pid
		}
	}, function(result) {
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		//清空子节点后重新异步加载
		treeObj.removeChildNodes(treeNode);
		treeObj.addNodes(treeNode, result.data, false);
	});
}

/**
 * 弹出form表单用于新增或者编辑
 * isAdd: 是否新增
 * id: 当前id
 */
function showForm(isAdd, id) {
	var submotFn = function() {
		var $form = $form = $("form", this.$content);
		var validate = $form.data("validator");
		if (validate && !$form.valid()) { //如果有验证 但是验证不通过
			return false;
		}
		$form.ajaxSubmit({
			url: base_url + "/system/dict/save",
			dataType: 'json',
			type: 'post',
			success: function(data) {
				if (data && data.code == 0) {
					$.alert("操作成功", "保存成功");
					if(isAdd){
						triggerClickTreeNode(id);
					}else {
						triggerClickTreeNode($("#rightMenu").data("pid"));
					}
				}
			},
			error: function(jqXHR, textStatus, errorMsg) { // 出错时默认的处理函数
				$.alert("请求失败:" + jqXHR.status + ":" + errorMsg, "请求失败");
			}
		});
	};
	var comfirmOpt = {
		columnClass: 'col-md-6 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1',
		onContentReady: function() {
			// after dialog conent ready  load form data
			var $form = $("form", this.$content);
			$nojs.validation.init(this.$content);
			if (isAdd) {
				$("#pid", $form).val(id);
			} else {
				var url = base_url + "/system/dict/detail";
				$request.get(url, {
					async: false,
					data: {
						id: id
					}
				}, function(result) {
					var formVm = new Vue({
						el: $form.get(0),
						data: result.data
					});
					//vue操作dom后需重新绑定validate
					$nojs.validation.init(this.$content);
				});
			}
		}
	};
	common.confirm($("div.hide form").prop("outerHTML"), submotFn, comfirmOpt);
}
/**右击菜单树*/
function OnRightClick(e, treeId, treeNode) {
	if(!treeNode){
		return ; 
	}
	var pnode = treeNode.getParentNode();
	if(pnode){
		$("#rightMenu").data("pid", pnode.id);
	}else {
		$("#rightMenu").data("pid", null);
	}
	$("#rightMenu").data("id", treeNode.id);
	$("#rightMenu").css({
		"z-index": 1,
		"position": "absolute",
		"top": (e.pageY) + "px",
		"left": (e.pageX) + "px"
	}).show();
	$("body").bind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event){
	if (!(event.target.id == "rightMenu" || $(event.target).parents("#rightMenu").length>0)) {
		hideMenu();
	}
}
function hideMenu(){
	$("#rightMenu").hide();
	$("body").unbind("mousedown", onBodyMouseDown);
}
function add(){
	hideMenu();
	showForm(true, $("#rightMenu").data("id"));
}
function edit(){
	hideMenu();
	showForm(false, $("#rightMenu").data("id"));
}
function del(){
	hideMenu();
	var id = $("#rightMenu").data("id");
	common.confirm("确定删除当前节点以及子节点吗?", function(){
		var url ="/system/dict/delete";
		$request.post(url, {data: {	id: id}}, function(result) {
			var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var node = zTreeObj.getNodeByParam("id", id, null);
			zTreeObj.removeNode(node);	
			triggerClickTreeNode($("#rightMenu").data("pid"));
			
				
		});
	});
}

/**触发一个节点的点击事件*/
function triggerClickTreeNode(id){
	var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
	if (id == 0 || !id || !zTreeObj) {
		$("#table").bootstrapTable('refresh');
		return;
	}
	var node = zTreeObj.getNodeByParam("id", id, null);
	zTreeObj.selectNode(node);
	zTreeObj.setting.callback.onClick(null, zTreeObj.setting.treeId, node);
}
